﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;
using Nemerle2.Compiler;

namespace Test
{
  /// <summary>
  /// Description of MethodTests.
  /// </summary>
  [TestFixture]
  public class MethodTests : AssemblyTests
  {
    [Test]
    public Test() : void
    {
      def findProperty(ty : ITypeInfo, name : string): IPropertyInfo
      {
        ty.GetMembers().First(member => member.Name == name && member.MemberKind == MemberKind.Property) :> IPropertyInfo;
      }

      def findMethod(ty : ITypeInfo, name : string): IMethodInfo
      {
        ty.GetMembers().First(member => member.Name == name && member.MemberKind == MemberKind.Method) :> IMethodInfo;
      }

      def ty = CommonProject.LoadedAssemblies.First().TopLevelTypes.Where(ty => ty.Name == typeof(TestAssembly.Program).Name).First();
      def prop = findProperty(ty, "Prop");
      def getter = findMethod(ty, "get_Prop");


      Assert.IsTrue (prop.Getter.HasValue);
      Assert.IsTrue (prop.Setter.HasValue);
      Assert.IsTrue (prop.IsStatic);
      Assert.IsFalse(prop.IsAbstract);
      Assert.AreSame(getter, prop.Getter.Value);

      def ty = CommonProject.LoadedAssemblies.First().TopLevelTypes.Where(ty => ty.Name == typeof(TestAssembly.AbstractClass).Name).First();
      def prop = findProperty(ty, "AbstractProp");
      Assert.IsTrue(prop.IsAbstract);

      match(prop.Type)
      {
        | t is FixedType.Class => Assert.AreEqual("Int32",t.info.Name)
        | _ => Assert.Fail()
      }
    }
  }
}
